# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from music_163_scrapy.items import SingerCategoryItem, SingerItem, SongItem, AlbumItem
import pymongo
import logging

class Music163ScrapyPipeline(object):

    def __init__(self, mongo_uri, mongo_db, mongo_user, mongo_passwd):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db
        self.mongo_user = mongo_user
        self.mongo_passwd = mongo_passwd

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_uri = crawler.settings.get('MONGO_URI'),
            mongo_db = crawler.settings.get('MONGO_DB'),
            mongo_user = crawler.settings.get('MONGO_USER'),
            mongo_passwd = crawler.settings.get('MONGO_PASSWD')
        )
    
    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]
        # self.db.authenticate(self.mongo_user, self.mongo_passwd)

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        if isinstance(item, SingerCategoryItem):
            SCDict = dict(item)
            try:
                self.db['singer-category'].update({
                    'singerCateId': SCDict['singerCateId']
                }, SCDict, True)
            except Exception as e:
                logging.error(e)
            pass
        elif isinstance(item, SingerItem):
            SingerDict = dict(item)
            self.db['singer'].update({
                'singerId': SingerDict['singerId']
            }, SingerDict, True)
            pass
        elif isinstance(item, SongItem):
            SongDict = dict(item)
            singerIds = SongDict['singerIds']
            singerNames = SongDict['singerNames']
            ret = self.db['song'].find_one({'songId': SongDict['songId']})
            resultIds = list()
            resultNames = list()
            if ret is not None:
                if ret['singerIds'] is not None and singerIds in ret['singerIds']:
                    resultIds = ret['singerIds']
                elif ret['singerIds'] is not None and singerIds not in ret['singerIds']:
                    ret['singerIds'].append(singerIds)
                    resultIds = ret['singerIds']
                else:
                    resultIds.append(singerIds)
                
                if ret['singerNames'] is not None and singerNames in ret['singerNames']:
                    resultNames = ret['singerNames']
                elif ret['singerNames'] is not None and singerNames not in ret['singerNames']:
                    ret['singerNames'].append(singerNames)
                    resultIds = ret['singerNames']
                else:
                    resultNames.append(singerNames)
            else:
                resultIds.append(singerIds)
                resultNames.append(singerNames)
            # print(ret)
            # print(resultIds, resultNames)
            self.db['song'].update({
                'songId': SongDict['songId']
            }, {
                'songId': SongDict['songId'],
                'songName': SongDict['songName'],
                'songCreateAt': SongDict['songCreateAt'],
                'songUrl': SongDict['songUrl'],
                'singerIds': resultIds,
                'singerNames': resultNames,
                'albumName': SongDict['albumName'] if 'albumName' in SongDict else '',
                'albumId': SongDict['albumId'] if 'albumId' in SongDict else ''
            }, True)
        elif isinstance(item, AlbumItem):
            AlbumDict = dict(item)
            self.db['album'].update({
                'albumId': AlbumDict['albumId']
            }, AlbumDict, True)
            pass
        else:
            print('未匹配')
        return item
